package com.tong.hao.ac.service.component.inner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tong.hao.ac.api.innerapi.api.IAcService;
import com.tong.hao.ac.db.entity.AcAccountClass;
import com.tong.hao.ac.db.entity.AcBillCategory;
import com.tong.hao.ac.db.entity.AcBillClass;
import com.tong.hao.ac.db.service.IAcAccountClassService;
import com.tong.hao.ac.db.service.IAcBillCategoryService;
import com.tong.hao.ac.db.service.IAcBillClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName AcService
 * @Author 友野浩二
 * @Date 2020/4/26 18:49
 * @Description 账单服务
 * @Version 1.0
 */
@Service
public class AcServiceImpl implements IAcService {

    /**
     * 账单类型表 服务类
     */
    @Resource
    private IAcBillCategoryService acBillCategoryServiceImpl;

    /**
     * 账单类别 服务类
     */
    @Resource
    private IAcBillClassService acBillClassServiceImpl;

    /**
     * 账户类型表 服务类
     */
    @Resource
    private IAcAccountClassService acAccountClassServiceImpl;

    /**
     * @return java.lang.Boolean
     * @Author 友野浩二
     * @Description // 判断参数id是否正确
     * @Date 2020/5/7 15:44
     * @Param [id, methodName]
     */
    @Override
    public Boolean isContains(Object value, String methodName) {
        if (value == null) {
            return false;
        }

        if (value instanceof String) {
            String id = (String) value;
            switch (methodName) {
                case IS_CONTAINS_BILL_CATEGORY:
                    return isContainsBillCategory(id);
                case IS_CONTAILS_BILL_CLASS:
                    return isContainsBillClass(id);
                case IS_CONTAILS_ACCOUNT_CLASS:
                    return isContainsAccountClass(id);
                default:
                    return false;
            }
        }
        return true;
    }

    /**
     * @return java.lang.Boolean
     * @Author 友野浩二
     * @Description // 根据账单类型编号查询是否存在对应的账单类型
     * @Date 2020/4/26 19:50
     * @Param [billId 账单类型编号]
     */
    @Override
    public Boolean isContainsBillCategory(String billId) {
        QueryWrapper<AcBillCategory> acBillCategoryQw = new QueryWrapper<>();
        acBillCategoryQw.lambda()
                .select(AcBillCategory::getBillId)
                .eq(AcBillCategory::getBillId, billId)
                .eq(AcBillCategory::getBillLevel, "2")
                .eq(AcBillCategory::getBillStatus, "Y");

        return this.acBillCategoryServiceImpl.getOne(acBillCategoryQw) != null;
    }

    /**
     * @return java.lang.Boolean
     * @Author 友野浩二
     * @Description // 根据账单类别编号查询是否存在对应的账单类别
     * @Date 2020/4/26 19:46
     * @Param [billId 账单类别编号]
     */
    @Override
    public Boolean isContainsBillClass(String billId) {
        QueryWrapper<AcBillClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AcBillClass::getBillId, billId);

        return this.acBillClassServiceImpl.getOne(queryWrapper) != null;
    }

    /**
     * @return java.lang.Boolean
     * @Author 友野浩二
     * @Description // 根据账户类型编号判断是否存在对应的账户类型
     * @Date 2020/5/7 19:10
     * @Param [accountId]
     */
    @Override
    public Boolean isContainsAccountClass(String accountId) {
        QueryWrapper<AcAccountClass> acAccountClassQw = new QueryWrapper<>();

        Map<String, Object> params = new LinkedHashMap<>(3);
        // key 为 数据库表字段名
        params.put("account_id", accountId);
        params.put("account_level", 2);
        params.put("account_status", "Y");
        acAccountClassQw.allEq(params, Boolean.FALSE);

        return this.acAccountClassServiceImpl.getOne(acAccountClassQw) != null;
    }
}
